<template>
	<view>
		<view v-if="noUserLocationPermission" class="no-auth-box">
			<up-empty mode="permission" text="没有定位权限,请点击下方按钮设置"></up-empty>
			<view style="margin-top: 32px;">
				<button @click="showRefuseLocationPermission">我知道了</button>
			</view>
		</view>
		<!-- <u-button text="获取WiFi" @click="initWifi"></u-button> -->
		<view v-else>
			<up-cell-group>
				<up-cell v-for="(item, index) in wifiList" :key="index">
					<template #title>
						<text>{{ item.SSID }}</text>
					</template>
					<template #right-icon>
						<button type="primary" @click="handleSelect(item)">选择</button>
					</template>
				</up-cell>
			</up-cell-group>
		</view>
		<text v-if="errMsg">{{ errMsg }}</text>
		<up-loading-page :loading="loading" :loading-text="loadingText"></up-loading-page>
	</view>
</template>

<script setup>
	import {
		getQueryParams
	} from '@/common/utils/index.js'
	import useAppStore from '@/store/app.js'

	const appStore = useAppStore()
	const eventBusName = ref('')
	const loading = ref(false)
	const loadingText = ref('')
	const wifiList = ref([])
	const deviceInfo = computed(() => appStore.deviceInfo)
	const noUserLocationPermission = ref(false)
	const errMsg = ref('')
	// const deviceInfo = computed(() => JSON.stringify(appStore.deviceInfo))

	function handleSelect(wifi) {
		console.log('选择WiFi了', wifi)
		uni.$emit(eventBusName.value, wifi)
		uni.navigateBack()
	}

	function onGetWifiList() {
		wx.onGetWifiList(function(res) {
			// 这里是监听获取到 Wi-Fi 列表后的回调函数
			console.log('监听到获取到 Wi-Fi 列表:', res);
			wifiList.value = res.wifiList
			// 在这里可以进一步处理 Wi-Fi 列表数据  
			// 比如更新页面数据，或者其他业务逻辑  
		});
	}

	function getWifiList() {
		loading.value = true
		loadingText.value = '正在获取WiFi信息...'
		wx.getWifiList({
			success: function(res) {
				// 获取 Wi-Fi 列表成功  
				console.log('获取 Wi-Fi 列表成功:', res);
				// wifiList.value = res.wifiList
				// 在这里可以处理 Wi-Fi 列表数据  
			},
			fail: function(err) {
				// 获取 Wi-Fi 列表失败  
				console.error('获取 Wi-Fi 列表失败:', err);
				errMsg.value = err.errMsg
			},
			complete() {
				loading.value = false
				loadingText.value = ''
			}
		});
	}

	function initWifi() {
		loading.value = true
		loadingText.value = '正在初始化...'
		wx.startWifi({
			success: function() {
				// Wi-Fi 模块初始化成功  
				console.log('Wi-Fi 模块已初始化');
				if (deviceInfo.isIOS) {
					getWifiList()
				} else {
					uni.authorize({
						scope: 'scope.userLocation',
						success() {
							console.log("用户已授权位置信息")
							getWifiList()
						},
						fail() {
							console.log("用户未授权位置信息")
							showRefuseLocationPermission()
							noUserLocationPermission.value = true
						}
					})
				}
				// 在这里可以调用 wx.getWifiList  
			},
			fail: function(err) {
				// 初始化失败  
				console.error('Wi-Fi 初始化失败:', err);
				errMsg.value = err.errMsg
			},
			complete() {
				loading.value = false
			}
		});
	}

	function showRefuseLocationPermission() {
		const that = this;
		uni.showModal({
			title: "提示",
			content: "需要获取用户位置权限",
			confirmText: "前往设置",
			showCancel: false,
			success(res) {
				if (res.confirm) {
					uni.openSetting({
						success: (res) => {
							console.log("打开设置成功", res);
							if (res.authSetting['scope.userLocation']) {
								console.log('成功授权userLocation')
								noUserLocationPermission.value = false
								getWifiList()
							} else {
								console.log('用户未授权userLocation')
								noUserLocationPermission.value = true
							}
						},
						fail: (err) => {
							console.log("打开设置失败", err)
						}
					})
				}
			}
		})
	}

	onShow(() => {
		onGetWifiList()
	})

	onMounted(() => {
		eventBusName.value = getQueryParams('eventBusName')
		initWifi()
	})
</script>

<style lang="scss" scoped>
	.no-auth-box {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		height: 100vh;
	}
</style>